//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Mail.Common.Components;
using Telligent.Registration;
using TWC = Telligent.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;

namespace CommunityServer.ControlPanel.BlogAdmin
{

	public class SectionEditControl : BaseBlogAdminControl
	{

		#region Public Properties

		private int sectionID = -1;
		public int SectionID
		{
			get { return sectionID; }
			set { sectionID = value;}
		}
		private int groupID = -1;
		public int GroupID
		{
			get { return groupID; }
			set { groupID = value;}
		}


		private Weblog weblog;
		public Weblog Weblog
		{
			get {return weblog;}
			set {weblog = value;}
		}

		#endregion

		#region Child Controls
		
		protected DirectoryWriter WeblogDirectoryWriter;
		protected TWC.TabbedPanes SettingTabs;
		protected SectionEditDetailsControl SectionDetails1;
		protected SectionPermissionListControl SectionPermissions1;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected Control LicenseMaxed;
		protected YesNoRadioButtonList LEEnableMailingList;
		
		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			CSContext csContext = CSContext.Current;
			
			SectionDetails1.SectionID = SectionPermissions1.SectionID = SectionID;
			SectionDetails1.GroupID = GroupID;

			if(!Page.IsPostBack)
			{
				
				WeblogDirectoryWriter.Visible = WeblogConfiguration.Instance().CreateBlogDirectories;

				foreach(TWC.TabbedPane tab in SettingTabs.Panes)
					if(tab.ID == "MailingList")
						tab.Visible = csContext.SiteSettings.EnableMailGateway;

				if (csContext.SiteSettings.EnableMailGateway)
				{
					MailingList list = MailingLists.GetMailingList(SectionID);
					if(MailingLists.GetMailingLists().Count < MailGateway.NumberOfLists || ((list != null) && list.IsMailingList))
					{
						if(list != null)
							LEEnableMailingList.Items.FindByValue( list.IsMailingList.ToString() ).Selected = true;
						else
							LEEnableMailingList.SelectedValue = false;
					}
					else
					{
						LEEnableMailingList.Enabled = false;
						LicenseMaxed.Visible = true;
					}
				}
			}
			
		}

		public bool Save()
		{
			if(!Page.IsValid)
				return false;

			SectionDetails1.Save() ;
			if(!Page.IsValid)
				return false;
			
			SectionPermissions1.SectionID = SectionDetails1.SectionID;
			SectionPermissions1.Save();
			if(!Page.IsValid)
				return false;

			// Update the LE settings
			if (CSContext.Current.SiteSettings.EnableMailGateway)
			{
				MailingList origList = MailingLists.GetMailingList(SectionDetails1.SectionID);
				if(MailingLists.GetMailingLists().Count < MailGateway.NumberOfLists || (origList != null && origList.IsMailingList))
				{
					// Set the core mailing list settings
					MailingList list = new MailingList();
					list.SectionID = SectionDetails1.SectionID;
					list.IsMailingList = LEEnableMailingList.SelectedValue;

					// Make sure the page is valid before saving.
					if(Page.IsValid)
						MailingLists.UpdateMailingList(list);
					else
						return false;
				}
			}

			return true;
		}

	}
}
